package com.github.tetrisgame.ui.activity;

import android.app.AlertDialog;
import android.media.AudioAttributes;
import android.media.MediaPlayer;
import android.media.SoundPool;
import android.os.Bundle;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import com.github.tetrisgame.R;
import com.github.tetrisgame.databinding.ActivityMainBinding;
import com.github.tetrisgame.databinding.LayoutGameOverBinding;
import com.github.tetrisgame.manager.GameCenter;
import com.github.tetrisgame.utils.BitmapUtils;


public class MainActivity extends AppCompatActivity {
    private ActivityMainBinding mBinding;
    private boolean mIsStarted = false;

    private MediaPlayer mediaPlayer;
    private SoundPool soundPool;

    private int rotateSoundID;
    private int moveSoundID;
    private int clearSoundID;
    private int fastDownSoundID;
    private int gameOverSoundID;
    private int levelUpSoundID;

    private AlertDialog dialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        mBinding = ActivityMainBinding.inflate(getLayoutInflater());
        setContentView(mBinding.getRoot());
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });
        //预先加载图片
        BitmapUtils.defaultUtils.init(this);

        //绑定视图
        GameCenter.defaultCenter.bindView(mBinding.previewView,mBinding.gameView);
        GameCenter.defaultCenter.initGame();

        //加载背景音乐
        loadBGM();

        //加载音效
        loadSound();

        //预加载弹窗
        initDialog();

        initEvent();
    }

    //加载背景音乐
    private void loadBGM(){
        mediaPlayer = MediaPlayer.create(this,R.raw.bgm1);
        mediaPlayer.setVolume(0.5f,0.5f);
        mediaPlayer.setLooping(true);
    }

    private void loadSound(){
        AudioAttributes audioAttributes = new AudioAttributes.Builder()
                .setUsage(AudioAttributes.USAGE_GAME)
                .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
                .build();
        soundPool = new SoundPool.Builder()
                .setAudioAttributes(audioAttributes)
                .setMaxStreams(2)
                .build();
        //旋转
        rotateSoundID = soundPool.load(this,R.raw.rotate1,0);
        //上下左右
        moveSoundID = soundPool.load(this,R.raw.move,0);
        //消行
        clearSoundID = soundPool.load(this,R.raw.clear1,0);
        //快速下落的
        fastDownSoundID = soundPool.load(this,R.raw.fast_down1,0);
        //游戏结束的
        gameOverSoundID = soundPool.load(this,R.raw.game_over,0);
        //等级提升
        levelUpSoundID = soundPool.load(this,R.raw.level_up,0);
    }

    private void initEvent() {
        mBinding.left.setOnClickListener(v ->{
            if (mIsStarted) {
                soundPool.play(moveSoundID,1.f,1f,0,0,1f);
                GameCenter.defaultCenter.moveLeft();
            }
        });
        mBinding.right.setOnClickListener(v ->{
            if (mIsStarted) {
                soundPool.play(moveSoundID,1.f,1f,0,0,1f);
                GameCenter.defaultCenter.moveRight();
            }
        });
        mBinding.down.setOnClickListener(v ->{
            if (mIsStarted) {
                soundPool.play(moveSoundID,1.f,1f,0,0,1f);
                GameCenter.defaultCenter.moveDown();
            }
        });
        mBinding.down.setOnLongClickListener(v ->{
            if (!mIsStarted) return false;
            soundPool.play(fastDownSoundID,1.f,1f,0,0,1f);
            GameCenter.defaultCenter.dropDown();
            return true;
        });
        mBinding.rotate.setOnClickListener(v ->{
            if (!mIsStarted) return;
            soundPool.play(rotateSoundID,1.f,1f,0,0,1f);
            GameCenter.defaultCenter.rotate();
        });
        mBinding.startBtn.setOnClickListener(v ->{
            if (mIsStarted == true){
                mIsStarted = false;
                mBinding.startBtn.setImageResource(R.drawable.play);
                GameCenter.defaultCenter.stop();
                mediaPlayer.pause();
            }else{
                mIsStarted = true;
                mBinding.startBtn.setImageResource(R.drawable.stop);
                GameCenter.defaultCenter.start();
                mediaPlayer.start();
            }
        });

        //监听游戏结束的事件
        GameCenter.defaultCenter.setOnGameOverListener(()->{
            soundPool.play(gameOverSoundID,1.f,1f,0,0,1f);

            //弹出一个提示框
            dialog.show();

            mIsStarted = false;
            mBinding.startBtn.setImageResource(R.drawable.play);
            GameCenter.defaultCenter.stop();
            mediaPlayer.pause();
        });
        GameCenter.defaultCenter.setOnScoreChangeListener(score ->{
            mBinding.tvScore.setText(score+"");
            soundPool.play(clearSoundID,1.f,1f,0,0,1f);
        });
        GameCenter.defaultCenter.setOnLevelChangeListener( level ->{
            mBinding.tvLevel.setText(level+"");
            soundPool.play(levelUpSoundID,1.f,1f,0,0,1f);
        });
    }

    //初始化弹框视图
    private void initDialog(){
        //配置弹窗的内容
//        AlertDialog.Builder builder = new AlertDialog.Builder(this);
//        builder.setTitle("提示");
//        builder.setMessage("是否退出游戏");
//        builder.setIcon(R.drawable.blue);
//        builder.setPositiveButton("确认",(dialog,which)->{
//
//        });
//        builder.setNegativeButton("取消",(dialog,which)->{
//
//        });

        //自定义弹窗内容
        AlertDialog.Builder builder = new AlertDialog.Builder(this);

        LayoutGameOverBinding gameOverBinding = LayoutGameOverBinding.inflate(getLayoutInflater());
        builder.setView(gameOverBinding.getRoot());
        gameOverBinding.btnReplay.setOnClickListener(v -> {
            dialog.dismiss();
            GameCenter.defaultCenter.restart();
            mIsStarted = true;
            mBinding.startBtn.setImageResource(R.drawable.stop);
            mediaPlayer.start();
        });
        //创建弹窗对象
        dialog = builder.create();
        dialog.setCanceledOnTouchOutside(false);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mediaPlayer != null) mediaPlayer.release();
        if (soundPool != null) soundPool.release();
    }
}













